package dk.lector;

import dk.lector.domain.DocFacade;
import dk.lector.domain.TeamShareMobile;
import dk.lector.providers.CACHEDDOC_VIEWContentProvider;
import dk.lector.providers.DOC_METADATAContentProvider;
import dk.lector.providers.DOC_STOREDContentProvider;
import dk.lector.providers.FAVORITESContentProvider;
import dk.lector.providers.SearchViewContentProvider;
import dk.lector.utils.IconLoader;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class DocumentsActivity extends BaseFullScreenActivity implements OnItemClickListener{

	private String TAG = this.getClass().getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		
		setContentView(R.layout.list_layout);
		TeamShareMobile core = (TeamShareMobile) getApplicationContext();
		
		Cursor resultCursor = DocFacade.getLocalDocuments(getBaseContext());
		resultCursor.setNotificationUri(getContentResolver(),
				DOC_STOREDContentProvider.CONTENT_URI);

		startManagingCursor(resultCursor);

		String[] from = new String[] { CACHEDDOC_VIEWContentProvider._ID,
				CACHEDDOC_VIEWContentProvider.TITLE };
		int[] to = new int[] { R.id.searchresult_item_id,
				R.id.searchresult_item_title };

		ListAdapter adapter = new SimpleCursorAdapter(DocumentsActivity.this,
				R.layout.doclist_layout_entry, resultCursor, from, to) {

			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				super.bindView(view, context, cursor);

				int icon = cursor.getInt(cursor
						.getColumnIndex(SearchViewContentProvider.ICON_ID));
				ImageView image = (ImageView) view
						.findViewById(R.id.imageresult_item_id);
				image.setImageResource(IconLoader.parseType(icon,
						DocumentsActivity.this));
			}
		};
		ListView list = ((ListView) findViewById(R.id.doc_listView));
		list.setAdapter(adapter);
		list.setClickable(true);
		list.setOnItemClickListener(this);

	}

	@Override
	public String actionBarText() {
		return getString(R.string.documents_activity_text);
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView tv = (TextView) arg1.findViewById(R.id.searchresult_item_id);
		Log.d("ONCLICK", "" + Integer.parseInt((String) tv.getText()));
		Intent getDoc = new Intent(this, DocumentLoaderActivity.class);
		getDoc.putExtra(DOC_METADATAContentProvider._ID, Integer.parseInt((String) tv.getText()));
		startActivity(getDoc);	
		
	}

}
